package com.rkc.controller;


import com.rkc.enums.BizCodeEnum;
import com.rkc.request.AddressAddRequest;
import com.rkc.service.AddressService;
import com.rkc.utils.JsonData;
import com.rkc.vo.AddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author 冉堃赤
 * @since 2021-10-07
 */
@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/api/address/v1")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @GetMapping("list")
    @ApiOperation("查询全部收货地址")
    public JsonData findUserAllAddresses() {
        return JsonData.buildSuccess(addressService.listUserAllAddresses());
    }

    @PostMapping("add")
    @ApiOperation("新增收货地址")
    public JsonData add(@ApiParam("地址对象") @RequestBody AddressAddRequest addressAddRequest) {
        addressService.add(addressAddRequest);
        return JsonData.buildSuccess();
    }

    @GetMapping("find/{address_id}")
    @ApiOperation("根据id查找地址详情")
    public JsonData detail(@ApiParam(value = "地址id", required = true) @PathVariable("address_id") Long id) {
        AddressVO addressVO = addressService.detail(id);
        return addressVO == null ? JsonData.buildResult(BizCodeEnum.ADDRESS_NO_EXITS) : JsonData.buildSuccess(addressVO);
    }

    @ApiOperation("删除地址")
    @DeleteMapping("del/{address_id}")
    public JsonData delete(@ApiParam(value = "地址id", required = true) @PathVariable("address_id") int addressId) {
        return addressService.delete(addressId) == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.ADDRESS_DEL_FAIL);
    }
}

